import HttpConfig from './httpConfig';
import axios from 'axios'
import Qs from 'qs'

let httpInterface = {};

function requestGet(path, params, successCallback, failCallback) {
  axios.defaults.withCredentials = false

  axios.get(HttpConfig.serverUrl + path, {

    "params": params

  }).then(successCallback)

    .catch(function (error) {
      failCallback && failCallback(error);
    });
}

function requestPost(path, params, successCallback, failCallback = null, isFormData = true) {
  axios.defaults.withCredentials = false;

  if (isFormData == true) {

    axios.post(HttpConfig.serverUrl + path, Qs.stringify(params), {
      headers: {
        'Content-Type': 'application/x-www-form-urlencoded',
        'Accept': 'application/json;charset=UTF-8'
      }

    })
      .then(successCallback)

      .catch(function (error) {
        failCallback && failCallback(error);
      });

  } else {
    axios.post(HttpConfig.serverUrl + path, params)

      .then(successCallback)

      .catch(function (error) {
        failCallback && failCallback(error);
      });
  }
}

httpInterface.requestGet = requestGet;
httpInterface.requestPost = requestPost;


httpInterface.URL_CLASS_LIST = "/class/list";  // 监控列表
httpInterface.URL_SUBJECT_LIST = "/subject/list";  // 课程列表
httpInterface.URL_CLASS_ALL = "/class/all";  // 班级列表

httpInterface.URL_CAMERA_LIST = "/camera/list";  // 设备分页列表
httpInterface.URL_CAMERA_ADD = "/camera/add";     // 添加监控设备
httpInterface.URL_CAMERA_UPDATE = "/camera/update";  // 更新监控设备
httpInterface.URL_CAMERA_DELETE = "/camera/delete";  // 删除监控设备

httpInterface.URL_RULE_LIST = "/rule/list";  // 规则分页查询
httpInterface.URL_RULE_ADD = "/rule/add";  // 添加规则
httpInterface.URL_RULE_UPDATE = "/rule/update";  // 更新规则
httpInterface.URL_RULE_DELETE = "/rule/delete";  // 规则删除
httpInterface.URL_RULE_GET_RULE_INFO = "/rule/getRuleInfo";  // 获得单条规则详情

httpInterface.URL_STUDENT_LIST = "/student/list";  // 学生列表
httpInterface.URL_TEACHER_LIST = "/teacher/list";  // 教师列表
httpInterface.URL_COURSE_LIST = "/course/list";  // 课程表数据

export default httpInterface;
